/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.uta.tsrh.notes.controllers;

import edu.uta.tsrh.patient.misc.DBMgrProxy;
import edu.uta.tsrh.patient.misc.PatientRecord;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author David
 */
public class RecordController {

    public static ArrayList<PatientRecord> getPatientRecordList(String fpatientId, String fpatientName, String fDOB) {
        ArrayList<PatientRecord> record;
        String patientID = "";
        if (fpatientId != null) {
            if (!fpatientId.isEmpty()) {
                patientID = fpatientId;
            }
        } else {
            patientID = "";
        }

        String patientFirstName = " ";
        String patientLastName = " "; 
        String[] names = null;
        if (fpatientName != null) {
            if (!fpatientName.isEmpty()) {
                if (fpatientName.contains(",")) {
                    names = fpatientName.split(",");
                } else if (fpatientName.contains(" ")) {
                    names = fpatientName.split(",");
                } else {
                    names = new String[2];
                    names[0] = "";
                    names[1] = fpatientName;
                }
            }
        }
        if (names != null) {
                patientLastName = names[0];
                patientFirstName = names[1];
        }

        Date patientDateOfBirth = null;
        if (fDOB != null) {
            if (!fDOB.isEmpty()) {
                DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
                try {
                    patientDateOfBirth = new Date(dateFormat.parse(fDOB).getTime());
                } catch (ParseException ex) {
                    Logger.getLogger(RecordController.class.getName()).log(Level.SEVERE, null, ex);
                }
            } else {
                patientDateOfBirth = null;
            }
        }
        record = DBMgrProxy.getInstance().getPatientRecordList(patientID, patientFirstName, patientLastName, patientDateOfBirth);
        
        return record;
    }
    
    
    public static PatientRecord getPatientRecord(String fpatientId) {
        return DBMgrProxy.getInstance().getPatientRecord(fpatientId);
    }
    
    /**
     *
     * @param pRecord
     */
    public static int setNewPatientRecord(PatientRecord pRecord){
       int value=0;
        value = DBMgrProxy.getInstance().addNewPatientRecord(pRecord);
        
        return value;
     }    
   
    public static boolean checkAvailibity(String patientId)
    {
        boolean status;
        System.out.println("patient id in Record controller "+ patientId);
        status = DBMgrProxy.getInstance().checkPatientAvailibity(patientId);
        System.out.println("status in Record controller "+ status);
        return status;
    }
    
    public static void setPhysicalRecord(PatientRecord precord)
    {
        String location ="        ";
        String fileStatus = "IN";
        String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
        System.out.println(timeStamp);
        String fileID = precord.getId();
        System.out.println(fileID);
        System.out.println(fileStatus);
        System.out.println(location);
        DBMgrProxy.getInstance().setPhysicalFileRecord(location,fileStatus,fileID,timeStamp);
    }
}